package com.star.security.context;

import com.star.security.authentication.Authentication;

/**
 * 令牌上下文持有者
 *
 * @author yangzj
 */
public class TokenContextHolder {

    private static TokenContextHolderStrategy strategy;

    static {
        initialize();
    }

    private static void initialize() {
        strategy = new ThreadLocalTokenContextHolderStrategy();
    }

    /**
     * 清除上下文
     */
    public static void clearContext() {
        strategy.clearContext();
    }

    /**
     * 获取上下文
     *
     * @return {@link Authentication}
     */
    public static Authentication getContext() {
        return strategy.getContext();
    }

    /**
     * 设置上下文
     *
     * @param context 上下文
     */
    public static void setContext(Authentication context) {
        strategy.setContext(context);
    }

    /**
     * 获取主体的身份
     *
     * @return {@link Object}
     */
    public static Object getPrincipal() {
        Authentication authentication = getContext();
        return authentication == null ? null : authentication.getPrincipal();
    }

}
